package cn.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.common.ExceResult;
import cn.common.LayTable;
import cn.filter.LoginCheck;
import cn.kpi.entity.civilass.CivilAssItemEntity;
import cn.kpi.entity.sys.OrgEntity;
import cn.kpi.service.civilass.ICivilAssItemService;

@Controller
@RequestMapping("civilassitem")
public class CivilAssItemController extends BaseController {

	@Autowired
	private ICivilAssItemService iCivilAssItemService;
	
	@RequestMapping("/civilass_item_list")
	public String CivilAssItemList(Model model) {
		return "civilass/civilass_item_list";
	}
	
	
	
	
	@RequestMapping(value = "/add_caitem")
	public String AddCAItem(HttpServletRequest request,HttpServletResponse response){
		try{
			CivilAssItemEntity entity=new CivilAssItemEntity();
			entity.setCassitenid(Long.parseLong(request.getParameter("cassitenid")));
			entity.setRemark(request.getParameter("remark"));
			entity.setItemname(request.getParameter("itemname").trim());
			entity.setItemtype(Integer.parseInt(request.getParameter("itemtype")));
			entity.setQz(Integer.parseInt(request.getParameter("qz")));
			entity.setScore(Integer.parseInt(request.getParameter("score")));
			
			int iResult=0;
			if(entity.getCassitenid()>0){
				iResult=iCivilAssItemService.UpdateItem(entity);
			}
			else{
				iResult=iCivilAssItemService.InsertCivilAssItem(entity);
			}
			
		
			return renderStringScript(response,"<script>window.parent.CloseDialog("+iResult+")</script>");
		}
		catch(Exception ex){
			return renderStringScript(response,"<script>window.parent.CloseDialog(0)</script>");
		}
	}
	@ResponseBody
	@RequestMapping(value = "/delete_caitem")
	public ExceResult DeleteCAItem(HttpServletRequest request,HttpServletResponse response){
		ExceResult result=new ExceResult();
		result.setCode(400);
		result.setResult(0);
		try{
		 Long cassitenid=Long.parseLong(request.getParameter("cassitenid"));
		 int iResult=iCivilAssItemService.DeleteItem(cassitenid);
		 if(iResult>0){
				result.setCode(200);
				result.setResult(iResult);
				result.setMsg("操作成功");
			}
			else{
				result.setCode(300);
				result.setResult(iResult);
				result.setMsg("操作失败");
			}
		}
		catch(Exception ex){
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}
	
	@LoginCheck(description = true)
	@ResponseBody
	@RequestMapping(value = "/get_list")
	public LayTable<CivilAssItemEntity> GetList(HttpServletRequest request) {

		List<CivilAssItemEntity> lst=null;
		LayTable<CivilAssItemEntity> layTable=new LayTable<CivilAssItemEntity>();
		try {
			String RoleName = request.getParameter("rolename");
			
			String where=" AND itemtype=0";
			
			
			lst = iCivilAssItemService.GetItemListByWhere(where);
			for (int i = 0; i < lst.size(); i++) {
				lst.get(i).setRowNum(i + 1);
			}

			layTable.setCode(0);
			layTable.setCount(100);
			layTable.setData(lst);
			
		} catch (Exception e) {
			layTable.setCode(500);
		}
		return layTable;
	}
}
